#    Copyright 2012 Tri-County Electric Cooperative, St. Matthews, South Carolina
#    This file is part of COOPIP.
#    COOPIP is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#    COOPIP is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#    You should have received a copy of the GNU General Public License
#    along with COOPIP.  If not, see <http://www.gnu.org/licenses/>.

#************************BEGIN Add Include Files to Path************************
import sys
IncludePath="/opt/coopip/djangoproject/include"
if(not IncludePath in sys.path):
    sys.path.append(IncludePath)

    
#************************END   Add Include Files to Path************************
from django.contrib.gis.geos import *
import os, time, psycopg2, datetime

#********************Setup psycopg2 and django section********************
PathDjangoRoot="/opt/coopip/"
if(not PathDjangoRoot in sys.path):
    sys.path.append(PathDjangoRoot)
from django.core.management import setup_environ
import djangoproject.settings
setup_environ(djangoproject.settings)
from djangoproject.models import *
from django.contrib.gis.geos import GEOSGeometry
import CIS, AMR, OMS, GIS
from django.contrib.gis.db import models
#********************Setup psycopg2 and django section********************
from django.db import transaction



import time

SerialNumbers = list(AMR.Serialnumber.objects.all().order_by('-serialnumber'))
for i in SerialNumbers:
    cismeter = CIS.Meterdetl.objects.filter(meter=str(i.serialnumber).rjust(12, '0')).filter(idlecode='00')
    if(cismeter):
        #print "Found CIS Meter Match"
        cislocation = CIS.Locinfodetl.objects.filter(location=cismeter[0].location)[0]
        cismeter = cismeter[0]
    else:
        print "No CIS Meter Match Found, SKIPPING: " + str(i.serialnumber)
        continue
    optimumObjectReference=AMR.OptimumObjectReference.objects.filter(serialnumber=i.serialnumber)
    if(optimumObjectReference):
        optimumObjectReference = optimumObjectReference[0]
    else:
        print "Just Created:  " + str(i.serialnumber)
        optimumObjectReference = AMR.OptimumObjectReference()
        optimumObjectReference.serialnumber = i.serialnumber
    #print "OldMeterNumber".ljust(30) + str(optimumObjectReference.meternumber).ljust(20) + 'NewMeterNumber'.ljust(30) + str(i.serialnumber).ljust(20)
    optimumObjectReference.meternumber=str(i.serialnumber)
    #print "OldUtilacctid".ljust(30) + str(optimumObjectReference.utilacctid).ljust(20) + 'NewUtilacctid'.ljust(30) + str(1)
    optimumObjectReference.utilacctid=1
    #print "OldMeter_objectid".ljust(30) + str(optimumObjectReference.meter_objectid).ljust(20) + 'Newmeter_objectid'.ljust(30) + str(i.serialnumber)
    optimumObjectReference.meter_objectid=str(i.serialnumber)
    #print "Oldservicelocation_objectid".ljust(30) + str(optimumObjectReference.servicelocation_objectid).ljust(20) + 'Newservicelocation_objectid'.ljust(30) + str(cislocation.location).lstrip('0')
    optimumObjectReference.servicelocation_objectid = str(cislocation.location).lstrip('0')
    #print "Oldcustomer_objectid".ljust(30) + str(optimumObjectReference.customer_objectid).ljust(20) + 'Newcustomer_objectid'.ljust(30) + str(cismeter.mbrsep)[:-3].lstrip('0')
    optimumObjectReference.customer_objectid = str(cismeter.mbrsep)[:-3].lstrip('0')
    #print "Oldupdated_at".ljust(30) + str(optimumObjectReference.updated_at).ljust(20) + 'Newupdated_at'.ljust(30) + str(datetime.datetime.now().date())
    optimumObjectReference.updated_at = datetime.datetime.now().date()
    optimumObjectReference.save()
    #print "Just Saved:  " + str(i.serialnumber)

    
    

    
                                            

